package com.apes.fn.tms.repository;

import com.apes.fn.tms.model.CarDetails;
import com.apes.fn.tms.model.CarInformation;
import com.apes.fn.tms.model.Waybill;
import com.apes.fn.tms.model.WaybillCargoDetails;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface WaybillCargoDetailsRepository extends CustomRepository<WaybillCargoDetails, String> {

    List<WaybillCargoDetails> findAllByWaybillId(Waybill waybillId);

    @Query(value = "SELECT DISTINCT C.PRODUCT_ID, C.PRODUCT_UOM_ID, C.LOADING_QTY, " +
            "       (SELECT NAME FROM MAS_PRODUCT WHERE ID = C.PRODUCT_ID) PRODUCT_NAME, " +
            "       (SELECT NAME FROM MAS_PRODUCT_UOM WHERE ID = C.PRODUCT_UOM_ID) UOM_NAME, " +
            "       (SELECT ORIGIN FROM PUR_PURCHASE_ORDER WHERE ID = (SELECT PURCHASE_ORDER FROM TMS_WAYBILL WHERE ID = C.WAYBILL_ID)) ORIGIN, " +
            "       (SELECT SOURCE_TYPE FROM PUR_PURCHASE_ORDER WHERE ID = (SELECT PURCHASE_ORDER FROM TMS_WAYBILL WHERE ID = C.WAYBILL_ID)) SOURCE_TYPE " +
            "  FROM TMS_CAR_INFORMATION A, TMS_WAYBILL_PLANNING_PLAN B, " +
            "       TMS_WAYBILL_CARGO_DETAILS C " +
            " WHERE A.ID = B.CAR_INFOMATION_ID " +
            "   AND B.WAYBILL_ID = C.WAYBILL_ID " +
            "   AND A.ID = :id ", nativeQuery = true)
    List<Map> qryWaybillCargoDetails(@Param("id") String id);
}
